//
//  LYSegmentView.m
//  SelectCity
//
//  Created by liuyan on 2019/3/19.
//  Copyright © 2019年 liuyan. All rights reserved.
//

#import "LYSegmentView.h"

#define index_width 13.0
#define index_height 13.0

@interface LYSegmentView()

@property (nonatomic, strong) UIView *vLineRed;
@property (nonatomic, strong) UISegmentedControl *segmentedControl;

@end

@implementation LYSegmentView

- (void)setSegmentWithBackground:(UIColor *)background titleArray:(NSArray *)titleArr titleFont:(UIFont *)fontSize titleLineSelectColor:(UIColor *)titleSelectColor normal:(UIColor *)normalColor withselectedIndex:(NSInteger)index {
    for (UIView*tmpV in self.subviews) {
        [tmpV removeFromSuperview];
    }
    BOOL isScr = YES;
    _titleArr = [[NSArray alloc]initWithArray:titleArr];
    _fontSize = fontSize;
    _View_W = self.frame.size.width;
    _itmeViewW = _View_W/titleArr.count;
    if (titleArr.count>1) {
        _customScr = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _customScr.backgroundColor = [UIColor clearColor];
        _customScr.showsHorizontalScrollIndicator = NO;
        _customScr.delegate = self;
        _customScr.contentSize = CGSizeMake(_View_W, self.frame.size.height);
        isScr = YES;
        [self addSubview:_customScr];
    } else {
        isScr = NO;
    }
    
    _segmentedControl = [[UISegmentedControl alloc]initWithFrame:CGRectMake(0, 0.5, _View_W, self.frame.size.height-1)];
    _segmentedControl.backgroundColor = COLOR_OF_CLEAR;
    _segmentedControl.tag = self.tag;
    [_segmentedControl setTintColor:COLOR_OF_CLEAR];
    
    if ( @available(iOS 13.0, *)) {
        _segmentedControl.selectedSegmentTintColor = COLOR_OF_CLEAR;
    } else {
        _segmentedControl.tintColor = COLOR_OF_CLEAR;
    }
    
    _segmentedControl.layer.borderColor = COLOR_OF_CLEAR.CGColor;
    for (NSInteger i = 0; i <titleArr.count; i ++) {
        [_segmentedControl insertSegmentWithTitle:titleArr[i] atIndex:i animated:NO];
    }
    [_segmentedControl setTitleTextAttributes:@{NSFontAttributeName:fontSize, NSForegroundColorAttributeName:normalColor} forState:UIControlStateNormal];
    CGFloat seleSize = fontSize.pointSize+2.0;
    if (self.frame.size.width<301) {
        seleSize = fontSize.pointSize+1.0;
    }
    
    [_segmentedControl setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:seleSize],NSForegroundColorAttributeName:titleSelectColor} forState:UIControlStateSelected];
    
    [_segmentedControl addTarget:self action:@selector(segmentedValueChanged:) forControlEvents:UIControlEventValueChanged];
    _segmentedControl.selectedSegmentIndex = index;
    //选项卡红色的标识条
    CGSize t_s = [_titleArr[index] sizeOfFont:_fontSize maxSize:CGSizeMake(_itmeViewW, 20)];
    
    _vLineRed = [[UIView alloc]initWithFrame:CGRectMake(index * _itmeViewW+(_itmeViewW-t_s.width/2)/2.0 , self.frame.size.height- 2, t_s.width/2, 3)];
    _vLineRed.backgroundColor = THEMECOLOR_OF_ORANGE;
    if (isScr==NO) {
        [self addSubview:_segmentedControl];
        [self addSubview:_vLineRed];
    } else {
        [_customScr addSubview:_segmentedControl];
        [_customScr addSubview:_vLineRed];
    }
    
    if (iOS13) {
        [_segmentedControl segmentedIOS13Style];
    }
}
- (void)segmentedValueChanged:(UISegmentedControl *)seg {
    CGSize t_s = [_titleArr[seg.selectedSegmentIndex] sizeOfFont:_fontSize maxSize:CGSizeMake(_itmeViewW, 20)];
    _vLineRed.frame = CGRectMake(seg.selectedSegmentIndex * _itmeViewW+(_itmeViewW-t_s.width/2)/2.0 , self.frame.size.height- 2, t_s.width/2, 3);
    
    if (_delegate && [_delegate respondsToSelector:@selector(customSegmentedValueChanged:)]) {
        [_delegate customSegmentedValueChanged:_segmentedControl];
    }
}
- (void)setselectedIndex:(NSInteger)index {
    NSString *title = _titleArr[index];
    CGSize t_s = [title sizeOfFont:_fontSize maxSize:CGSizeMake(_itmeViewW, 20)];
    _vLineRed.frame = CGRectMake(index * _itmeViewW+(_itmeViewW-t_s.width/2)/2.0, self.frame.size.height- 2, t_s.width/2, 3);
    _segmentedControl.selectedSegmentIndex = index;
}

//添加标记：
- (void)setRightTopLabelWithAry:(NSArray*)topNumAry {
    for (NSInteger i = 0; i<topNumAry.count; i++) {
        [[_customScr viewWithTag:100+i] removeFromSuperview];
        [[_customScr viewWithTag:200+i] removeFromSuperview];
    }
    
    for (NSInteger i= 0; i<topNumAry.count; i++) {
        NSString*titleStr = _titleArr[i];
        CGSize titleSize = [titleStr sizeOfFont:FontBold(Font_15) maxSize:CGSizeMake(_itmeViewW, 30)];
        NSString *text = topNumAry[i];
        UIView*superView = [[UIView alloc]initWithFrame:CGRectMake((_itmeViewW+titleSize.width)/2+_itmeViewW*i, 5, 5, 40)];
        superView.backgroundColor = [UIColor clearColor];
        [_customScr addSubview:superView];
        superView.tag = 100+i;
        
        if ((![text isEqualToString:@"0"]) && (![text isEqualToString:@""])) {
            [self createRightTopIndexLabelWithSize:CGSizeMake(index_width, index_height) tag:200+i borderWidth:1.5 borderColor:[UIColor redColor].CGColor text:text font:[UIFont systemFontOfSize:10] textColor:[UIColor whiteColor] SuperView:superView];
        }
    }
}

//右上角标记
- (UILabel *)createRightTopIndexLabelWithSize:(CGSize)size tag:(NSInteger)tag borderWidth:(CGFloat)borderWidth borderColor:(CGColorRef)borderColor text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor SuperView:(UIView *)superView{
    CGFloat w = size.height/2;
    CGRect superFrame = superView.frame;
    CGFloat x = CGRectGetWidth(superFrame)-4;
    CGFloat y = -2;
    CGFloat height = size.height;
    CGFloat width;
    if (text.length == 1) {
        width = size.width;
    } else if (text.length == 2) {
        width = size.width*1.5;
    } else {
        width = size.width*2;
    }
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 8, 8)];
    label.backgroundColor = THEMECOLOR_OF_RED;
    label.layer.cornerRadius = label.height/2;
    label.layer.masksToBounds = YES;
    label.layer.borderWidth = borderWidth;
    label.layer.borderColor = borderColor;
//    label.text = text;
    label.textColor = textColor;
    label.font = font;
    label.textAlignment = NSTextAlignmentCenter;
    [superView addSubview:label];
    return label;
}

@end


